//
//  CHEvaluationListController.m
//  CarFile
//
//  Created by 陈浩 on 2017/4/14.
//  Copyright © 2017年 陈浩. All rights reserved.
//

#import "CHEvaluationListController.h"
#import "CHEvaluationListCell.h"
@interface CHEvaluationListController ()<UITableViewDelegate,UITableViewDataSource>
@property (nonatomic ,strong) NSMutableArray *datas;
@property (nonatomic ,strong) UITableView *tableView;
@property (nonatomic ,assign) int page;

@end
@implementation CHEvaluationListController

-(NSMutableArray *)datas{
    
    if (!_datas) {
        _datas = [NSMutableArray array];
    }
    return _datas;
}



- (void)viewDidLoad {
    [super viewDidLoad];
    
    self.navigationController.navigationBar.hidden = NO;
    self.title = [NSString stringWithFormat:@"评价列表(%@)",self.model.areacn];
    
    
    [self setupTableView];
    
  
    
}

-(void)setupTableView{
    
    UITableView *tableView = [[UITableView alloc] initWithFrame:CGRectMake(0, 0, UIScreenW, UIScreenH - 64)];
    tableView.tableFooterView = [[UIView alloc] initWithFrame:CGRectZero];
    tableView.dataSource = self;
    tableView.delegate = self;
    
    // 下拉刷新
    tableView.mj_header= [MJRefreshNormalHeader headerWithRefreshingBlock:^{
        // 模拟延迟加载数据，因此2秒后才调用（真实开发中，可以移除这段gcd代码）
        
        [self getData];
    }];
    tableView.mj_footer = [MJRefreshAutoNormalFooter footerWithRefreshingBlock:^{
        
        [self loadMoreData];
    }];
    tableView.mj_footer.automaticallyHidden = YES;
    [tableView.mj_header beginRefreshing];
    [self.view addSubview:tableView];
    self.tableView = tableView;

}


-(void)getData{
    _page = 1;
    NSMutableDictionary *params = [NSMutableDictionary dictionary];
    params[@"areaid"] = self.model.areaid;
    params[@"usertype"] = [Util getValue:@"usertype"];
    params[@"user"] = [Globle getInstance].user;
    params[@"sign"] = [DESCript encrypt:self.model.areaid encryptOrDecrypt:kCCEncrypt key:[Globle getInstance].key];
    params[@"pagesize"] = @"20";
    params[@"pagenum"] = [NSString stringWithFormat:@"%d",_page];
    params[@"start"] = self.startTime;
    params[@"end"] = self.endTime;

    [[Globle getInstance].service requestWithServiceName:@"lcipappgovevaluatelist" params:params httpMethod:@"POST" resultIsDictionary:YES completeBlock:^(id result) {
    
        NSLog(@"-------%@",result);
        
        [self.datas removeAllObjects];
        [self.tableView.mj_header endRefreshing];
        if ([result[@"code"] isEqualToString:@"1"]) {
            for (NSDictionary *dict in result[@"data"][@"list"]) {
                CHEvaluationListModel *model = [CHEvaluationListModel objectWithKeyValues:dict];
                CHEvaluationListFrame *listFrm = [[CHEvaluationListFrame alloc] init];
                listFrm.model = model;
                [self.datas addObject:listFrm];
            }
            self.tableView.placeholderImage = @"data_null";
            self.tableView.placeholder = @"暂时没有数据哦~";
            [self.tableView reloadData];
        }
    }];
}


-(void)loadMoreData{
    
    _page++;
    NSMutableDictionary *params = [NSMutableDictionary dictionary];
    params[@"areaid"] = self.model.areaid;
    params[@"usertype"] = [Util getValue:@"usertype"];
    params[@"user"] = [Globle getInstance].user;
    params[@"sign"] = [DESCript encrypt:self.model.areaid encryptOrDecrypt:kCCEncrypt key:[Globle getInstance].key];
    params[@"pagesize"] = @"20";
    params[@"pagenum"] = [NSString stringWithFormat:@"%d",_page];
    params[@"start"] = self.startTime;
    params[@"end"] = self.endTime;
    
    [[Globle getInstance].service requestWithServiceName:@"lcipappgovevaluatelist" params:params httpMethod:@"POST" resultIsDictionary:YES completeBlock:^(id result) {
      
        [self.tableView.mj_footer endRefreshing];
        if ([result[@"code"] isEqualToString:@"1"]) {
            for (NSDictionary *dict in result[@"data"][@"list"]) {
                CHEvaluationListModel *model = [CHEvaluationListModel objectWithKeyValues:dict];
                CHEvaluationListFrame *listFrm = [[CHEvaluationListFrame alloc] init];
                listFrm.model = model;
                [self.datas addObject:listFrm];
            }
        
        }
        
        [self.tableView reloadData];
    }];
    
}


#pragma mark - UITableViewDataSource
-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    
    return self.datas.count;
    
}


-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    CHEvaluationListCell *cell = [CHEvaluationListCell cellForTableView:tableView];
    cell.evaluationFrm = self.datas[indexPath.row];

    return cell;
    
    
}


-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
    
    return [self.datas[indexPath.row] cellHeight];

    
}

#pragma mark LongriseRouterDelegater代理回调方法
-(void)setParams:(id)obj block:(LongriseRouterBlock)block
{
    
    if (obj && [obj isKindOfClass:[NSMutableDictionary class]]) {
        
        NSMutableDictionary *dict = obj;
        self.model = dict[@"model"];
        self.startTime = dict[@"starttime"];
        self.endTime = dict[@"endtime"];
  
    }
}



@end
